package cn.m3a1.service;

import cn.m3a1.bean.StudentOfClass;
import cn.m3a1.bean.StudentOfClassExample;
import cn.m3a1.bean.User;
import cn.m3a1.bean.UserExample;
import cn.m3a1.dao.StudentOfClassMapper;
import cn.m3a1.dao.UserMapper;
import cn.m3a1.util.GlobalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wyj
 * @date 2019/6/30
 * @time 上午11:19
 */
@Service
public class AdminService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    StudentOfClassMapper studentOfClassMapper;
    public List<User>  getUser(String username){

        UserExample userExample=new UserExample();
        if (!GlobalUtils.isEmpty(username)){
            UserExample.Criteria criteria = userExample.createCriteria();
            criteria.andUsernameEqualTo(username);
        }
        List<User> users = userMapper.selectByExample(userExample);
        return users;

    }
    public User  getUser(int  id){

        UserExample userExample=new UserExample();

        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(id);

        List<User> users = userMapper.selectByExample(userExample);
        return users.get(0);

    }
    public boolean  addUser(User user){
        List<User> users = getUser(user.getUsername());
        if (users.size()!=0){
            return false;
        }
        int insert = userMapper.insert(user);

        return insert==1;

    }

    public  boolean  updateUser(User user){
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(user.getId());
        int i = userMapper.updateByExampleSelective(user, userExample);
        return  i==1;
    }

    public  boolean  delUser(int id){
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(id);
        int i = userMapper.deleteByExample(userExample);
        return  i==1;
    }

    /**
     * 更改学生班级
     * @return
     */
    public  boolean upStuClass(StudentOfClass studentOfClass){
        StudentOfClassExample studentOfClassExample=new StudentOfClassExample();
        StudentOfClassExample.Criteria criteria = studentOfClassExample.createCriteria();
        criteria.andStudentidEqualTo(studentOfClass.getStudentid());
        int i = studentOfClassMapper.updateByExampleSelective(studentOfClass, studentOfClassExample);
        return  i==1;
    }
}
